<?php

declare(strict_types=1);
/**
 * This file is part of MoChat.
 * @link     https://mo.chat
 * @document https://mochat.wiki
 * @contact  group@mo.chat
 * @license  https://github.com/mochat-cloud/mochat/blob/master/LICENSE
 */
namespace MoChat\App\ValidationCode\Logic;

use Hyperf\Di\Annotation\Inject;
use MoChat\App\User\Contract\UserContract;
use MoChat\App\ValidationCode\Contract\ValidationCodeContract;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use Qbhy\HyperfAuth\AuthManager;

/**
 * 新增验证码
 *
 * Class StoreLogic
 */
class StoreLogic
{
    /**
     * @Inject
     * @var UserContract
     */
    protected $userService;

    /**
     * @Inject
     * @var ValidationCodeContract
     */
    protected $validationCodeService;

    /**
     * @Inject
     * @var AuthManager
     */
    protected $authManager;

    /**
     * @param array $params 请求参数
     * @return array 响应数组
     */
    public function handle(array $params): array
    {
        # # 验证手机号
        if ($params['type'] == 0) {
            $phoneUser = $this->userService->getUsersByPhone([$params['phone']], ['id']);
            if (! $phoneUser) {
                throw new CommonException(ErrorCode::INVALID_PARAMS, '手机号未注册，请先注册此手机号');
            }
        }

        $this->validationCodeService->createValidationCode($params);

        return [];
    }
}
